#####################################################################
# Do Personality Traits Predict Voter Attitudes When Politics Is    #
# Structured Around Conflict? Lessons From Israel                   #
#                                                                   #
# Rivlin-Angert, Yakter, and Sheffer                                #
# Public Opinion Quarterly                                          #
#                                                                   #
# Replication code - Figure 2                                       #
#####################################################################
# To load the data, users should place the relevant csv	file in     #
# in their R working directory (or type the full file paths as      #
# saved on their computer).                                         #
#####################################################################

## Required packages
library(tidyr)
library(dplyr)
library(ggplot2)
library(scales)
library(ggrepel)

## load data
dat <- read.csv("Angert-Rivlin, Yakter, Sheffer POQ - figure 2 HEXACO and EPR data.csv")

## bivariate correlation
cor.test(dat$epr_share_sr_dominant,dat$r2_hexaco_only, method = "pearson")

## plot correlation (Figure 2)
plot.f2 <- ggplot(dat,aes(x=epr_share_sr_dominant, y=r2_hexaco_only)) + 
  geom_point() +
  geom_text_repel(aes(label=country_name), size=2.5, max.overlaps=12) +
  geom_smooth(method=lm, se=T, color="grey40", linewidth=0.8) +
  labs(x = "Population Share of Senior/Dominant Ethnic Groups",
       y = "Personality-Orientiation Model Fit") +
  scale_y_continuous(labels=c("0","0.05","0.1", "0.15", "0.2")) +
  scale_x_continuous(labels = scales::percent) +
  theme_bw() +
  theme(legend.position = "bottom",
        legend.box = "vertical",
        legend.title = element_text(size=10),
        strip.text.x = element_text(size = 11),
        axis.text = element_text(size=11),
        panel.grid = element_blank(),
  )

ggsave(file = "Figure 2.jpg", plot=plot.f2, width=6, height=4, dpi=900)


## additional test (mentioned in the manuscript in text only) - rich countries only
# code dummy for rich countries by World Bank data in the relevant year (2014)
dat.rich <- filter(dat,income_worldbank=="high")
# plot with second slopw just for rich countries (in blue)
ggplot(dat,aes(x=epr_share_sr_dominant, y=r2_hexaco_only)) + 
  geom_point(data=filter(dat,income_worldbank=="high"),shape=16) +
  geom_point(data=filter(dat,income_worldbank!="high"),shape=1) +
  geom_smooth(method=lm, se=FALSE, color="grey40",linewidth=0.8) +
  geom_smooth(data=subset(dat,income_worldbank=="high"),method=lm, se=FALSE, 
              color="blue",linetype="dashed") + 
  labs(x = "Population Share of Senior/Dominant Ethnic Groups",
       y = ~"Personality-Orientiation Model Fit") +
  scale_y_continuous(labels=c("0","0.05","0.1", "0.15", "0.2")) +
  scale_x_continuous(labels = scales::percent) +
  theme_bw() +
  theme(legend.position = "bottom",
        legend.box = "vertical",
        legend.title = element_text(size=10),
        strip.text.x = element_text(size = 11),
        axis.text = element_text(size=11),
        panel.grid = element_blank(),
  )
